package com.nanjia.boot.product.spec.group.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nanjia.boot.common.enums.StatusEnum;
import com.nanjia.boot.core.security.util.SecurityUtils;
import com.nanjia.boot.product.category.mapper.ProductCategoryMapper;
import com.nanjia.boot.product.category.model.entity.ProductCategory;
import com.nanjia.boot.product.spec.group.converter.SpecGroupConverter;
import com.nanjia.boot.product.spec.group.mapper.SpecGroupMapper;
import com.nanjia.boot.product.spec.group.model.entity.SpecGroup;
import com.nanjia.boot.product.spec.group.model.form.SpecGroupForm;
import com.nanjia.boot.product.spec.group.model.query.SpecGroupQuery;
import com.nanjia.boot.product.spec.group.model.vo.SpecGroupVO;
import com.nanjia.boot.product.spec.group.service.SpecGroupService;
import com.nanjia.boot.product.spu.mapper.ProductSpuMapper;
import com.nanjia.boot.product.spu.model.entity.ProductSpu;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

/**
 * 商品规格组服务实现类
 *
 * @author 周洋
 * @since 2025-07-09 15:30
 */
@Service
@RequiredArgsConstructor
public class SpecGroupServiceImpl extends ServiceImpl<SpecGroupMapper, SpecGroup> implements SpecGroupService {

    private final SpecGroupConverter specGroupConverter;

    private final SpecGroupMapper specGroupMapper;
    private final ProductSpuMapper productSpuMapper;

    private final ProductCategoryMapper productCategoryMapper;

    /**
     * 获取商品规格组分页列表
     *
     * @param queryParams 查询参数
     * @return {@link IPage<SpecGroupVO>} 商品规格组分页列表
     */
    @Override
    public IPage<SpecGroupVO> getSpecGroupPage(SpecGroupQuery queryParams) {
        Page<SpecGroupVO> pageVO = this.baseMapper.getSpecGroupPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }

    /**
     * 获取商品规格组表单数据
     *
     * @param id 商品规格组ID
     * @return 商品规格组表单数据
     */
    @Override
    public SpecGroupForm getSpecGroupFormData(Long id) {
        SpecGroup entity = this.getById(id);
        return specGroupConverter.toForm(entity);
    }

    /**
     * 新增商品规格组
     *
     * @param formData 商品规格组表单对象
     * @param ipAddr
     * @return 是否新增成功
     */
    @Override
    public boolean saveSpecGroup(SpecGroupForm formData, String ipAddr) {
        SpecGroup entity = specGroupConverter.toEntity(formData);
        entity.setCreateBy(SecurityUtils.getUserStrId());
        entity.setCreateIp(ipAddr);
        entity.setCreateTime(LocalDateTime.now());
        entity.setUpdateTime(LocalDateTime.now());
        return this.save(entity);
    }

    /**
     * 更新商品规格组
     *
     * @param id       商品规格组ID
     * @param formData 商品规格组表单对象
     * @param ipAddr
     * @return 是否修改成功
     */
    @Override
    public boolean updateSpecGroup(Long id, SpecGroupForm formData, String ipAddr) {
        SpecGroup entity = specGroupConverter.toEntity(formData);
        entity.setId(id);
        entity.setUpdateBy(JSONUtil.toJsonStr(SecurityUtils.getUserId()));
        entity.setUpdateIp(ipAddr);
        entity.setUpdateTime(LocalDateTime.now());
        return this.updateById(entity);
    }

    /**
     * 删除商品规格组
     *
     * @param ids 商品规格组ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteSpecGroups(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的商品规格组数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        List<SpecGroup> list = new ArrayList<>();
        for (Long l : idList) {
            SpecGroup specGroup = new SpecGroup();
            specGroup.setId(l);
            specGroup.setUpdateTime(LocalDateTime.now());
            specGroup.setUpdateBy(JSONUtil.toJsonStr(SecurityUtils.getUserId()));
            specGroup.setStatus(StatusEnum.DELETE.getValue());
            list.add(specGroup);
        }
        return this.updateBatchById(list, idList.size());
    }

    @Override
    public List<SpecGroup> getSpecGroupList(SpecGroupQuery queryParams) {
        Map<String, Object> map = new HashMap<>();
        map.put("spu_id", queryParams.getSpuId());
        map.put("status", 1);
        return specGroupMapper.selectByMap(map);
    }

}
